﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using Telerik.Framework.Search;
using Telerik.Cms.Engine;
using Telerik.News;

namespace SelArom.Net.Sitefinity.IndexProviders.LatestNews
{
    /// <summary>
    /// Indexes active (published and not expired) news items from the Default Provider of the News Module
    /// </summary>
    public class LatestNewsIndexProvider : IIndexingServiceClient
    {
        /// <summary>
        /// Defines the name of the provider. This name is used to mange providers within Indexing Service.
        /// </summary>
        public string Name
        {
            get
            {
                return "LatestNewsIndexProvider";
            }
        }

        /// <summary>
        /// Provides detailed description of the client
        /// </summary>
        public string Description
        {
            get
            {
                return "Provides indexing for current news.";
            }
        }

        /// <summary>
        /// Meta fields for this provider
        /// </summary>
        protected string[] MetaFields
        {
            get
            {
                return new[]{
					"Title"
				};
            }
        }

        public IIndexerInfo[] GetContentToIndex()
        {

            var list = new List<IIndexerInfo>();

			// get current events
			var mgr = new NewsManager(NewsManager.DefaultContentProvider);
			var filters = new IMetaSearchInfo[2];
			filters[0] = new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual);
			filters[1] = new MetaSearchInfo(MetaValueTypes.DateTime, "Publication_Date", DateTime.Now, SearchCondition.LessOrEqual);
        	var news = mgr.Content.GetContent("Publication_Date", filters);

            foreach (IContent item in news)
            {
				// only include published items
				if (item.Status != ContentStatus.Published) continue;

				// init meta keys
                var metaFields = new Hashtable();            
                foreach (var key in MetaFields)
                {
                    metaFields.Add(key, "");
                }
				metaFields["Title"] = item.GetMetaData("Title");

				// ensure url appkey is present and index item
				var newsUrl = ConfigurationManager.AppSettings[NewsManager.DefaultContentProvider + "Url"];
				if (string.IsNullOrEmpty(newsUrl)) break;
                list.Add(
                    new LatestNewsIndexerInfo(
                    string.Concat(newsUrl, item.UrlWithExtension.ToLower()),
                    metaFields,
                    item.Content.ToString(),
					item.ID));
            }

            return list.ToArray();
        }

        public string[] GetUrlsToIndex()
        {
            return new string[0];
        }

        public event EventHandler<IndexEventArgs> Index;

        public void Initialize(IDictionary<string, string> settings)
        {
        }
    }
}